<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/6
 * Time: 18:30
 */

namespace XiaoZhuBundle\Model;


class Product
{
    private $REQUEST;
    private $DOCTRINE;

    public function __construct($REQUEST,$DOCTRINE)
    {
        $this->REQUEST = $REQUEST->getCurrentRequest();
        $this->DOCTRINE = $DOCTRINE;
    }

    public function db()
    {
        return $this->DOCTRINE->getManager()->createQueryBuilder();
    }

    public function rep($entityName)
    {
        return $this->DOCTRINE->getManager()->getRepository($entityName);
    }

    public function listProduct($param)
    {
        $page = isset($param["page"]) ? $param["page"] : 1;
        $per_page = isset($param["per_page"]) ? $param["per_page"] : 10;
        $orderBy = isset($param["orderBy"]) ? $param["orderBy"] : array();
        $db = $this->listQuery($param);
        $db->select("
            p.id , p.name , p.cid , p.price , p.marketPrice
            , n.isNew , n.isHot , n.isSale , n.isFamous , n.isWebIndex
            ,m.create
            ,s.cover , s.img1 , s.img2 , s.img3 , s.info
            ,d.sell , d.collect
        ");
        foreach ( $orderBy as $key => $val ) {
            $val = $val == 1 ? "DESC" : "ASC";
            switch ($key) {
                case "create":
                    $form = "m";
                    break;
                case "sell":
                case "collect":
                    $form = "d";
                    break;
                default:
                    $form = "p";
            }
            $db->orderBy( "$form.$key" , $val );
        }
        $db->setFirstResult( $page * $per_page - $per_page );
        $db->setMaxResults( $per_page );
        $lists = $db->getQuery()->getArrayResult();
        foreach ($lists as &$product) {
            $product["price"] = $product["price"]/100;
            $product["marketPrice"] = $product["marketPrice"]/100;
        }
        return $lists;

    }

    public function countProduct($param)
    {
        $db = $this->listQuery($param);
        $db->select("
        count(p.id) as num
        ");
        $lists = $db->getQuery()->getSingleResult();
        return $lists["num"];
    }

    public function listQuery($param)
    {$strProduct = "AdminsBundle:Product";
        $strProductMeta = "AdminsBundle:ProductMeta";
        $strProductNote = "AdminsBundle:ProductNote";
        $strProductShow = "AdminsBundle:ProductShow";
        $strProductStatistic = "AdminsBundle:ProductStatistic";
        $andWhere = isset($param["andWhere"]) ? $param["andWhere"] : array();
        $db = $this->db();
        $db->from($strProduct , "p");
        $db->leftJoin("$strProductShow","s","WITH","p.id = s.id");
        $db->leftJoin("$strProductMeta","m","WITH","p.id = m.id");
        $db->leftJoin("$strProductNote","n","WITH","p.id = n.id");
        $db->leftJoin("$strProductStatistic","d","WITH","p.id = d.id");

        foreach ( $andWhere as $key => $val ) {
            switch ($key) {
                case "cid":
                    $cidStr = $this->getCategoryString($val);
                    //var_dump($cidStr);
                    $db->andWhere('p.searchInfo like :val' )
                        ->setParameter( "val" , "%$cidStr%" );
                    break;
                case "isSale":
                    if ($val == 1) {
                        $db->andWhere('p.price / p.marketPrice < :val' )
                            ->setParameter( "val" , "0.7" );//todo 折扣设置应该从产品配置取出
                    }
                    break;
                case 'attr':
                    //属性筛选
                    if (is_array($val)) {
                        foreach ($val as $v) {
                            $db->andWhere('p.searchInfo like :val' )
                                ->setParameter( "val" , "%,\"valId\":\"$v\"%" );
                        }
                    }
                    break;
                default://默认从标签中筛选
                    $db->andWhere( "n.$key = :val" )
                        ->setParameter( "val" , $val );
            }

        }
        return  $db;
    }

    private function getCategoryString($cid)
    {
        $rep = $this->rep("AdminsBundle:Category");
        $pid = $cid;
        do {
            $cat = $rep->find($pid);
            $pid = $cat->getParent();
            if ($pid != 0 ) {
                $cid .= ",".$pid;
            }
        } while ($pid != 0);
        $cid .= "]";
        return $cid;
    }

    public function showProduct($id)
    {
        $strProduct = "AdminsBundle:Product";
        $strProductMeta = "AdminsBundle:ProductMeta";
        $strProductNote = "AdminsBundle:ProductNote";
        $strProductShow = "AdminsBundle:ProductShow";
        $strProductStatistic = "AdminsBundle:ProductStatistic";
        $db = $this->db();
        $db->select("
            p.id , p.name , p.cid , p.price , p.marketPrice , p.searchInfo
            , n.isNew , n.isHot , n.isSale , n.isFamous , n.isWebIndex
            ,m.create
            ,s.cover , s.img1 , s.img2 , s.img3 , s.info , s.intro ,s.title
            ,d.sell , d.collect , d.stars , d.star1 , d.star2 , d.star3 , d.star4 , d.star5
        ");
        $db->from($strProduct , "p");
        $db->leftJoin("$strProductShow","s","WITH","p.id = s.id");
        $db->leftJoin("$strProductMeta","m","WITH","p.id = m.id");
        $db->leftJoin("$strProductNote","n","WITH","p.id = n.id");
        $db->leftJoin("$strProductStatistic","d","WITH","p.id = d.id");
        $db->where( "p.id = :id" );
        $db->setParameter( "id" , $id );
        $product = $db->getQuery()->getSingleResult();
        $product["price"] = $product["price"] / 100;
        $product["marketPrice"] = $product["marketPrice"] / 100;
        $product["intro"] = json_decode($product["intro"],true);

        return $product;
    }
}